import os
import re
import threading
import time

from pt1.assert_vd import assert_camera, assert_green
from pt1.bianli import filter_and_remove_files, filter_and_keep_minimum_continuous_frames
from pt1.co_vd import create_frame
from pt1.delete import delete_ml
from pt1.pt1_test import get_video_data, create_slow_video
from pt1.xlsx_t import process_files, green_word_write




'''
3.20思想，只传入原视频路径和要填的xlsx路径即可，优化了遍历函数，对绿文的前150和单个或者联系数不过10的跳过不处理
3.21

'''
def process(address: str,  xlsx_path: str, roi, word_roi):
    try:
        # 双线程解析
        original_name = os.path.basename(address)
        vd_name = re.sub(r'(_\d+)\.', r'\1_vd.', original_name)
        vd_path = os.path.join('/Users/zltest/Desktop/', vd_name)



        cap, fps, width, height, slow_fps, slow_video_name = get_video_data(address)
        cap_vd, fps_vd, width_vd, height_vd, slow_fps_vd, slow_video_name_vd = get_video_data(vd_path)



        vd = threading.Thread(target=create_slow_video, args=(slow_video_name_vd,
                                                              cap_vd,fps_vd,width_vd,height_vd,slow_fps_vd,
                                                              ))
        sxt = threading.Thread(target=create_slow_video, args=(slow_video_name,
                                                              cap,fps,width,height,slow_fps,
                                                              ))

        # 启动线程
        vd.start()
        sxt.start()

        # 等待所有线程完成
        vd.join()
        sxt.join()


    except Exception as e:
        print(f'读取并生成慢放视频失败，失败原因{e}')
        raise e

    try:
        vd_zhen = threading.Thread(target=create_frame, args=(slow_video_name_vd, ))
        sxt_zhen = threading.Thread(target=create_frame, args=(slow_video_name, ))
        # 启动线程
        vd_zhen.start()
        sxt_zhen.start()

        # 等待所有线程完成
        vd_zhen.join()
        sxt_zhen.join()

    except Exception as e:
        print(f'读取慢放视频为图片帧失败，失败原因{e}')
        raise e

    time.sleep(.5)

    end_name = os.path.basename(slow_video_name)

    numbers_split = end_name.strip()

    绿色文字_比对通过的图片路径 = None  # 初始化目标目录路径
    摄像头_比对通过的图片路径 = None
    全部图片_的图片路径 = None
    全部绿色文字图片_的图片路径 = None
    current_file_path = os.path.abspath(__file__)
    # 退回项目根目录
    current_dir_path = os.path.dirname(current_file_path)

    # 解析文件名以确定目标目录路径
    if 'wired' in numbers_split or '有线' in numbers_split:
        if '720' in numbers_split:
            绿色文字_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'green_word', 'wired_720')
            全部图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'static', 'wired_720')
            摄像头_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'will_assert', 'wired_720')
            全部绿色文字图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'vd_static', 'wired_720')
        elif '1080' in numbers_split:
            绿色文字_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'green_word', 'wired_1080')
            全部图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'static', 'wired_1080')
            摄像头_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'will_assert', 'wired_1080')
            全部绿色文字图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'vd_static', 'wired_720')
        elif '480' in numbers_split:
            绿色文字_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'green_word', 'wired_480')
            全部图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'static', 'wired_480')
            摄像头_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'will_assert', 'wired_480')
            全部绿色文字图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'vd_static', 'wired_720')

    elif 'wireless' in numbers_split or '无线' in numbers_split:
        if '720' in numbers_split:
            绿色文字_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'green_word', 'wireless_720')
            全部图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'static', 'wireless_720')
            摄像头_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'will_assert', 'wireless_720')
            全部绿色文字图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'vd_static', 'wireless_720')
        elif '1080' in numbers_split:
            绿色文字_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'green_word', 'wireless_1080')
            全部图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'static', 'wireless_1080')
            摄像头_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'will_assert', 'wireless_1080')
            全部绿色文字图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'vd_static', 'wireless_720')
        elif '480' in numbers_split:
            绿色文字_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'green_word', 'wireless_480')
            全部图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'static', 'wireless_480')
            摄像头_比对通过的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'will_assert', 'wireless_480')
            全部绿色文字图片_的图片路径 = os.path.join(os.path.dirname(current_dir_path), 'vd_static', 'wireless_720')

    try:
        '''
        多线程判断绿色文字和摄像头灰度
        '''
        # 创建线程
        camera_thread = threading.Thread(target=assert_camera, args=(roi, 全部图片_的图片路径))
        green_thread = threading.Thread(target=assert_green, args=(word_roi, 全部绿色文字图片_的图片路径))

        # 启动线程
        camera_thread.start()
        green_thread.start()

        # 等待所有线程完成
        camera_thread.join()
        green_thread.join()
        print('关于摄像头和绿色文字的图片断言对比已经完成')

    except Exception as e:
        raise e


    try:
        '''
        多线程梳理存放断言成功的文件名字
        '''
        # 创建线程
        # 摄像头取最大
        thread1 = threading.Thread(target=filter_and_remove_files, args=(摄像头_比对通过的图片路径, roi, 全部图片_的图片路径))

        # 绿色文字取最小
        thread2 = threading.Thread(target=filter_and_keep_minimum_continuous_frames, args=(绿色文字_比对通过的图片路径,))

        # 启动线程
        thread1.start()
        thread2.start()

        # 等待所有线程完成
        thread1.join()
        thread2.join()

        print("所有处理任务已完成")
    except Exception as e:
        raise e




    time.sleep(.5)
    # 写入到xlsx
    process_files(摄像头_比对通过的图片路径, xlsx_path, fps)

    time.sleep(.5)
    green_word_write(绿色文字_比对通过的图片路径, xlsx_path, fps)

    #写入到了xlsx后应该把static和will_assert清空


    delete_ml(绿色文字_比对通过的图片路径)
    delete_ml(摄像头_比对通过的图片路径)
    delete_ml(全部图片_的图片路径)
    delete_ml(全部绿色文字图片_的图片路径)


    print(f'进程{slow_video_name}执行完成！！')



